import AutoChart from '@/components/Echarts/AutoChart';
// @ts-ignore
import { medicinePrescription } from '@/services/api';
import { BorderBox1 } from '@jiaminghi/data-view-react';
import { useEffect } from 'react';
import { useImmer } from 'use-immer';
import './Chart4.less';

const ColumnCharts: React.FC = () => {
  const [options, setOptions] = useImmer({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#396993',
        },
      },
    },
    grid: {
      top: '4%',
      left: '7%',
      right: '4%',
      bottom: '8%',
      containLabel: true,
    },
    legend: {
      // left: 'left',
      textStyle: {
        color: '#fff',
      },
    },
    xAxis: {
      type: 'category',
      data: [],
      // axisLabel: {
      //   rotate: 315, // 将 X 轴文字旋转 45 度
      // },
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        data: [],
        type: 'bar',
        color: '#396993',
        showBackground: true,
        barGap: 10,
        itemStyle: {
          color: {
            type: 'linear',
            x: 1,
            y: 0,
            x2: 0,
            y2: 0,
            colorStops: [
              {
                offset: 0,
                color: '#66e4fe', // 0% 处的颜色
              },
              {
                offset: 1,
                color: '#396993', // 100% 处的颜色
              },
            ],
            global: false, // 缺省为 false
          },
          borderRadius: [40, 40, 0, 0],
        },
        backgroundStyle: {
          color: 'rgba(180, 180, 180, 0.2)',
        },
      },
    ],
  });

  useEffect(() => {
    medicinePrescription().then((res) => {
      setOptions((draft) => {
        draft.xAxis.data = res.data.map((i: API.ChartsData) => i.name);
        draft.series[0].data = res.data.map((i: API.ChartsData) => Number(i.value));
      });
    });
  }, []);
  return (
    <>
      <div className="'marginb-10">
        <BorderBox1 color={['#396993', '#396993']}>
          <div className="echart_title">
            <div>中药剂型统计</div>
          </div>
          <div className="chart4_height padding-5">
            <AutoChart option={options as any}></AutoChart>
          </div>
        </BorderBox1>
      </div>
    </>
  );
};
export default ColumnCharts;
